package uiLayer;

import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class ProductUI {
    private ProductCtr prodCtr;
    
    public ProductUI() {
        prodCtr = new ProductCtr();
    }
    
    /**
     * Product menu methods
     */
    public void productsMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeProductsMenu();
            
            switch(choice) {
            case 1 : createProduct();
                break;
            case 2 : findProduct();
                break;
            case 3 : updateProduct();
                break;
            case 4 : removeProduct();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    }
    
    private int writeProductsMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Product Menu ***");
        System.out.println("1. Add product ");
        System.out.println("2. Find product ");
        System.out.println("3. Update product ");
        System.out.println("4. Remove product ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Dvd input information
     */
    public int inputBarcode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the product (numeric value) : ");
        int barcode = keyboard.nextInt();
        return barcode;
    }
    
    private String inputName() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name of the product : ");
        String name = keyboard.nextLine();
         if(!name.isEmpty()) {
            return name;
        }
        else {
            return inputName();
        }
    }
    
    private double inputPrice() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the price : ");
        double price = keyboard.nextInt();
        return price;
    }
    
    private String inputLocationCode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the location code : ");
        String locationCode = keyboard.nextLine();
         if(!locationCode.isEmpty()) {
            return locationCode;
        }
        else {
            System.out.println("Please use 1 for the timber department or 2 for the DIY center");
            return inputLocationCode();
        }
    }
    
    private int inputInStock() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the number of items that will be added to the stock : ");
        int inStock = keyboard.nextInt();
        return inStock;
    }
    
    /**
     * Dvd show information
     */
    private void showName(String name)
    {
        System.out.println("Name : " + name);
    }
    
    private void showPrice(double price)
    { 
        System.out.println("Price : " + price);
    }
    
    private void showLocationCode(String locationCode)
    { 
        System.out.println("Location : " + locationCode);
    }
    
    /**
     * Find dvd method 
     **/
     private void findProduct() {
        int barcode = inputBarcode();
        Product prod = (prodCtr.findProduct(barcode));
        if (prod != null) {
            System.out.println();
            showName(prod.getName());
            showPrice(prod.getPrice());
            showLocationCode(prod.getLocationCode());
            System.out.println("In stock : " + prod.getInStock());
            System.out.println();
        }
        else {
            System.out.println("No product was found with the specified barcode.");
        }
        MainMenuUI.pause();
    }
    
    /**
     * Update product
     */
    private void updateProduct() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        Product prod = (prodCtr.findProduct(barcode));
        boolean stop = false;
        if (prod != null) {
            showName(prod.getName());
            showPrice(prod.getPrice());
            showLocationCode(prod.getLocationCode());
            
            System.out.println();
            System.out.println("1. Update barcode");
            System.out.println("2. Update name");
            System.out.println("3. Update price");
            System.out.println("4. Update location");
            System.out.println("5. Update number of items in stock");
            System.out.println("0. Return to previous menu");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {    
                case 1 : prod.setBarcode(inputBarcode());
                    break;
                case 2 : prod.setName(inputName());
                    break;
                case 3 : prod.setPrice(inputPrice());
                    break;
                case 4 : prod.setLocationCode(inputLocationCode());
                    break;
                case 5 : prod.setInStock(inputInStock());
                    break;
                case 0 : stop = true;
                    break;
                }
                System.out.println();
                System.out.println("The product information was successfully updated");
            }
        }
        
        else {
            System.out.println("No product was found with the specified barcode.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    /**
     * Create product method
     */
    private void createProduct() {
        int barcode = inputBarcode();
        String name = inputName();
        double price = inputPrice();
        String locationCode = inputLocationCode();
        int inStock = inputInStock();
        
        prodCtr.createProduct(name, barcode, price, locationCode, inStock);
        System.out.println();
        System.out.println("The product was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void removeProduct() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        Product prod = (prodCtr.findProduct(barcode));
        boolean stop = false;
        if (prod != null) {
            System.out.println();
            showName(prod.getName());
            showPrice(prod.getPrice());
            showLocationCode(prod.getLocationCode());
            System.out.println("In stock : " + prod.getInStock());
            
            System.out.println();
            System.out.println("1. Remove product");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while (!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : prodCtr.removeProduct(barcode);
                    System.out.println();
                    System.out.println("The product was successfully removed");
                    System.out.println();
                    MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No product was found with the specified barcode.");
        }
    }    
        
}
